package com.scaffold.photo.action;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.commons.config.AbstractConfig;
import com.scaffold.photo.entity.Photo;
import com.scaffold.photo.service.AlbumService;
import com.scaffold.photo.service.PhotoService;

/**
 * 照片操作xhtml
 * @author wangjg
 */
@Controller
public class PhotoAction extends BasicBizAction{
	@Autowired
	private PhotoService photoService;
	@Autowired
	private AlbumService albumService;
	@Autowired
	private AbstractConfig config;
	
	/**
	 * 保存照片
	 */
	@RequestMapping("/photo/pick.xhtml")
	public String pick(ModelMap model){
		Map<String, String> params = this.getParameterMap();
		String imgs = params.get("imgs");
		if(imgs!=null){
			String[] imgList = StringUtils.splitPreserveAllTokens(imgs, '#');
			model.put("imgList", imgList);	
		}
		String titles = params.get("titles");
		if(titles!=null && titles.length()>0){
			String[] titleList = StringUtils.splitPreserveAllTokens(titles, '#');
			model.put("titleList", titleList);
		}
		
		Map<String,String> remark = new LinkedHashMap<String,String>();
		remark.put("doc_title", params.get("doc_title"));
		remark.put("doc_href", params.get("doc_href"));
		remark.put("cookie", params.get("cookie"));
		model.put("remark", remark);
		
		return "/photo/pick.vm"; 
	}
	
	/**
	 * 保存照片
	 */
	@RequestMapping("/photo/savePick.xhtml")
	public String savePick(){
		List<Photo> list = this.getBeanList("path", Photo.class);
		this.photoService.save(list);
		String url = config.getString("url.photoPick.redirect");
		return "redirect:"+url;
	}
	
	/**
	 * 照片列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/photo/list.xhtml")
	public String list(ModelMap model, Long albumId, String title, String path, String remark, String keyword){
		Page page = this.getPage(20);
		List<Photo> list = photoService.pageQuery(page, albumId, title, path, remark, keyword);
		model.put("photoList", list);
		model.put("page", page);

		return "/photo/list.vm";
	}

	/**
	 * 编辑照片
	 * @param id 编号
	 */
	@RequestMapping("/photo/edit.xhtml")
	public String edit(ModelMap model, Long id){
		if(id!=null){
			Photo photo = this.photoService.getPhoto(id);
			this.checkOwner(photo);
			model.put("photo", photo);	
			model.put("_update", "Y");
		}
		return "/photo/edit.vm"; 
	}
	
	/**
	 * 保存照片
	 */
	@RequestMapping("/photo/save.xhtml")
	public String save(String _update, Long id){
		if(!"Y".equals(_update)){
			Photo photo = new Photo();
			this.bindParams(photo);
			this.setOwner(photo);
			this.photoService.save(photo);
		}else{
			Photo photo = this.photoService.getPhoto(id);
			this.checkOwner(photo);
			this.bindParams(photo);
			this.photoService.update(photo);
		}
		return "redirect:/photo/list.xhtml";
	}
	
	/**
	 * 删除照片
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/photo/delete.xhtml")
	public String delete(Long id){
		Photo photo = this.photoService.getPhoto(id);
		this.checkOwner(photo);
		this.photoService.delete(photo);
		return "redirect:/photo/list.xhtml";
	}

}
